package com.nbstar.film.spider.thread;

import com.alibaba.fastjson.JSON;
import com.nbstar.film.spider.domain.ProxyInfo;
import com.nbstar.film.spider.http.SipderStringRequest;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.function.Function;

@Slf4j
public class ProxyThread extends Thread {
    public static LinkedBlockingQueue<ProxyInfo>  ipPool = new LinkedBlockingQueue<>();

    private static SipderStringRequest  stringRequest = new SipderStringRequest();

    private static Map<String, Function<String, String>> proxyGetIpList = new HashMap<>();

    private boolean run = true;
    private static boolean localProxy = false;

    public static void deleteProxyHandler(ProxyInfo proxyInfo) {
        ipPool.remove(proxyInfo);
        String deleteUrl = "http://127.0.0.1:5010/delete/?proxy=" + JSON.toJSONString(proxyInfo);
        try {
            if (localProxy) {
                String result = stringRequest.getHtml(deleteUrl);
                log.info("Delete proxy:{}" + result);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public String LocalThreadPoolHandler(String url) {
        localProxy = true;
        String html = null;
        if (ipPool.size() > 0) {
            return null;
        }
        try {
            html = stringRequest.getHtml(url);
            System.out.println(html);

            ProxyInfo proxyInfo = (ProxyInfo) JSON.parseObject(html, ProxyInfo.class);
            html = proxyInfo.getProxy();
            ipPool.put(proxyInfo);
            System.out.println(html);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return html;
    }

    public String UUHTTPHandler(String url) {
        String html = null;
        if (ipPool.size() > 0) {
            return null;
        }
        try {
            html = stringRequest.getHtml(url);
            if (html.contains("\"success\":false")) {
                System.out.println(html);
                return "http://" +html;
            }
            String ips[] = html.split("\r\n");

            for (String ip : ips) {
                ProxyInfo info = new ProxyInfo();
                info.setProxy("http://" + ip);

                ipPool.put(info);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }



        return "http://" + html;
    }

    public String TxtResultHander (String url)  {
        String html = null;
        if (ipPool.size() > 0) {
            return null;
        }
        try {
            html = stringRequest.getHtml(url);
            if (html.contains("\"success\":false")) {
                System.out.println(html);
                return "http://" +html;
            }
            String ips[] = html.split("\r\n");

            for (String ip : ips) {
                ProxyInfo info = new ProxyInfo();
                info.setProxy("http://" + ip);

                ipPool.put(info);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }



        return "http://" +html;
    }

    public ProxyThread() {
        //proxyGetIpList.put("http://http.tiqu.alicdns.com/getip3?num=20&type=1&pro=&city=0&yys=0&port=1&pack=110824&ts=0&ys=0&cs=0&lb=1&sb=0&pb=4&mr=1&regions=", this::TxtResultHander);
        //proxyGetIpList.put("http://http.tiqu.alicdns.com/getip3?num=20&type=1&pro=&city=0&yys=0&port=1&pack=110824&ts=0&ys=0&cs=0&lb=1&sb=0&pb=4&mr=1&regions=&gm=4", this::TxtResultHander);
        //proxyGetIpList.put("http://d.jghttp.golangapi.com/getip?num=20&type=1&pro=&city=0&yys=0&port=1&pack=27586&ts=0&ys=0&cs=0&lb=1&sb=0&pb=4&mr=1&regions=", this::TxtResultHander);
        //proxyGetIpList.put("http://http.tiqu.alicdns.com/getip3?num=20&type=1&pro=&city=0&yys=0&port=11&pack=110935&ts=0&ys=0&cs=0&lb=1&sb=0&pb=4&mr=1&regions=&gm=4", this::TxtResultHander);
        //proxyGetIpList.put("http://http.tiqu.alicdns.com/getip3?num=20&type=1&pro=&city=0&yys=0&port=11&pack=110935&ts=0&ys=0&cs=0&lb=1&sb=0&pb=4&mr=1&regions=", this::TxtResultHander);
        //proxyGetIpList.put("http://http.tiqu.alicdns.com/getip3?num=1&type=1&pro=&city=0&yys=0&port=1&pack=110824&ts=0&ys=0&cs=0&lb=1&sb=0&pb=4&mr=1&regions=&gm=4", this::TxtResultHander);
        //proxyGetIpList.put("http://http.tiqu.alicdns.com/getip3?num=1&type=1&pro=&city=0&yys=0&port=11&pack=110824&ts=0&ys=0&cs=0&lb=1&sb=0&pb=4&mr=1&regions=&gm=4", this::TxtResultHander);
        //proxyGetIpList.put("http://http.tiqu.alicdns.com/getip3?num=20&type=1&pro=&city=0&yys=0&port=11&pack=110824&ts=0&ys=0&cs=0&lb=1&sb=0&pb=4&mr=1&regions=&gm=4", this::TxtResultHander);
        //proxyGetIpList.put("http://api.wandoudl.com/api/ip?app_key=53e18b973a6118af52d204978b0b2f8c&pack=0&num=20&xy=2&type=1&lb=\\r\\n&mr=1&", this::TxtResultHander);
        //proxyGetIpList.put("http://http.tiqu.alicdns.com/getip3?num=20&type=1&pro=&city=0&yys=0&port=11&pack=110824&ts=0&ys=0&cs=0&lb=1&sb=0&pb=45&mr=2&regions=&gm=4", this::UUHTTPHandler);
        //proxyGetIpList.put("http://piping.mogumiao.com/proxy/api/get_ip_bs?appKey=63432323043e4134b2e57313afa99012&count=10&expiryDate=0&format=2&newLine=2", this::UUHTTPHandler);
        //proxyGetIpList.put("http://api.wandoudl.com/api/ip?app_key=c43a61e60994cf19a042ec79fe003d8c&pack=0&num=1&xy=2&type=1&lb=\\r\\n&mr=2&", this::UUHTTPHandler);
        //proxyGetIpList.put("http://http.tiqu.alicdns.com/getip3?num=1&type=1&pro=&city=0&yys=0&port=11&pack=110824&ts=0&ys=0&cs=0&lb=1&sb=0&pb=4&mr=1&regions=&gm=4", this::UUHTTPHandler);
        //proxyGetIpList.put("http://piping.mogumiao.com/proxy/api/get_ip_bs?appKey=1e30f93d33204a3d957bd3b1480380ab&count=20&expiryDate=0&format=2&newLine=2", this::UUHTTPHandler);
        proxyGetIpList.put("http://piping.mogumiao.com/proxy/api/get_ip_al?appKey=a2992ef1c65d47688b488b61fa6b6ca8&count=5&expiryDate=0&format=2&newLine=2", this::UUHTTPHandler);
        //proxyGetIpList.put("http://127.0.0.1:5010/get/", this::LocalThreadPoolHandler);
    }

    public void shutdown() {
        run = false;
    }

    @Override
    public void run() {

        while (run) {
            requestProxyIp();
        }
    }

    public static void requestProxyIp() {
      
        for (Map.Entry<String, Function<String, String>> entry : proxyGetIpList.entrySet()) {
            entry.getValue().apply(entry.getKey());
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
